home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / dalla rivista / host contacted / jikes.lha / jikes-1.11 / src / segment.cpp < prev    next >
C/C++ Source or Header  |  1999-10-15  |  4KB  |  149 lines

  1. // $Id: segment.cpp,v 1.3 1999/10/15 02:30:42 shields Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1999, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10. #include "config.h"
  11. #include "segment.h"
  12.  
  13. u2 &PairSegment::Image(u2 target)
  14. {
  15.     if (array == NULL)
  16.     {
  17.         for (int i = 0; i < top; i++)
  18.         {
  19.             if (list[i].target == target)
  20.                 return list[i].value;
  21.         }
  22.  
  23.         if (top < (int)LIST_LIMIT)
  24.         {
  25.             int j = top++;
  26.             list[j].target = target;
  27.             list[j].value = 0;
  28.             return list[j].value;
  29.         }
  30.  
  31.         unsigned offset = ((unsigned) target) & MASK;
  32.         array = (u2 *) memset(new u2[BLKSIZE], 0, BLKSIZE * sizeof(u2));
  33.         array -= offset;
  34.  
  35.         for (int j = 0; j < top; j++)
  36.             array[list[j].target] = list[j].value;
  37.     }
  38.  
  39.     return array[target];
  40. }
  41.  
  42.  
  43. u2 &Pair::operator[](const u2 target)
  44. {
  45.     int k = ((unsigned) target) >> PairSegment::LOG_BLKSIZE;
  46.  
  47.     if (k >= base_size)
  48.     {
  49.         int old_base_size = base_size;
  50.         PairSegment **old_base = base;
  51.  
  52.         //
  53.         // For the first allocation, assume that there won't be that many more.
  54.         // If there are others add a much bigger margin.
  55.         //
  56.         base_size = k + (old_base_size == 0 ? 2 : 16);
  57.         base = new PairSegment*[base_size];
  58.  
  59.         if (old_base != NULL)
  60.         {
  61.             memmove(base, old_base, old_base_size * sizeof(PairSegment *));
  62.             delete [] old_base;
  63.         }
  64.  
  65.         memset(&base[old_base_size], 0, (base_size - old_base_size) * sizeof(PairSegment *));
  66.     }
  67.  
  68.     if (! base[k])
  69.         base[k] = segment_pool.AllocatePairSegment();
  70.  
  71.     return base[k] -> Image(target);
  72. }
  73.  
  74.  
  75. Pair &TripletSegment::Image(u2 target)
  76. {
  77.     if (array == NULL)
  78.     {
  79.         for (int i = 0; i < top; i++)
  80.         {
  81.             if (list[i].target == target)
  82.                 return *list[i].value;
  83.         }
  84.  
  85.         if (top < (int)LIST_LIMIT)
  86.         {
  87.             int j = top++;
  88.             list[j].target = target;
  89.             return *(list[j].value = segment_pool.AllocatePair());
  90.         }
  91.  
  92.         unsigned offset = ((unsigned) target) & MASK;
  93.         array = (Pair **) memset(new Pair*[BLKSIZE], 0, BLKSIZE * sizeof(Pair *));
  94.         array -= offset;
  95.  
  96.         for (int j = 0; j < top; j++)
  97.             array[list[j].target] = list[j].value;
  98.     }
  99.  
  100.     return *(array[target] ? array[target] : array[target] = segment_pool.AllocatePair());
  101. }
  102.  
  103.  
  104. u2 &Triplet::Image(const u2 target, const u2 target2)
  105. {
  106.     int k = ((unsigned) target) >> TripletSegment::LOG_BLKSIZE;
  107.  
  108.     if (k >= base_size)
  109.     {
  110.         int old_base_size = base_size;
  111.         TripletSegment **old_base = base;
  112.  
  113.         base_size = k + 4;
  114.         base = new TripletSegment*[base_size];
  115.  
  116.         if (old_base != NULL)
  117.         {
  118.             memmove(base, old_base, old_base_size * sizeof(TripletSegment *));
  119.             delete [] old_base;
  120.         }
  121.  
  122.         memset(&base[old_base_size], 0, (base_size - old_base_size) * sizeof(TripletSegment *));
  123.     }
  124.  
  125.     if (! base[k])
  126.         base[k] = segment_pool.AllocateTripletSegment();
  127.  
  128.     return base[k] -> Image(target)[target2];
  129. }
  130.  
  131.  
  132. SegmentPool::SegmentPool() : triplet_segment_pool(1024),
  133.                              pair_segment_pool(4096),
  134.                              pair_pool(4096)
  135. {}
  136.  
  137.  
  138. SegmentPool::~SegmentPool()
  139. {
  140.     for (int i = 0; i < triplet_segment_pool.Length(); i++)
  141.         delete triplet_segment_pool[i];
  142.  
  143.     for (int j = 0; j < pair_segment_pool.Length(); j++)
  144.         delete pair_segment_pool[j];
  145.  
  146.     for (int k = 0; k < pair_pool.Length(); k++)
  147.         delete pair_pool[k];
  148. }
  149.